package directory.model;

import controller.ParametroMgr;
import model.BasicBean;
import java.sql.ResultSet;

public class PersonRelationship extends BasicBean {

    public String dateFrom = "";
    public String dateTo = "";
    public String idPersonA = "";
    public String idPersonB = "";
    public String idPersonRelationType = "";

    public PersonRelationship() {
    }

    public PersonRelationship(ResultSet rs) {
        try {
            id = validateString(rs.getString("id"));
            idPersonRelationType = validateString(rs.getString("idPersonRelationType"));
            idPersonA = validateString(rs.getString("idPersonA"));
            idPersonB = validateString(rs.getString("idPersonB"));
            dateFrom = validateDate(rs.getString("dateFrom"));
            dateTo = validateDate(rs.getString("dateTo"));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public String getHtmlRow() {
        String sz = "";
        sz += "<td class='st4middle'>" + ParametroMgr.mgr.getItemFromSql("select id, name from PersonRelationType where id=" + idPersonRelationType).nombre + "</td>";
        sz += "<td class='st4middle'>" + ParametroMgr.mgr.getItem("Person", idPersonA).nombre + "</td>";
        sz += "<td class='st4middle'>" + ParametroMgr.mgr.getItem("Person", idPersonB).nombre + "</td>";
        sz += "<td class='st4middle'>" + dateFrom + "</td>";
        sz += "<td class='st4middle'>" + dateTo + "</td>";
        return sz;
    }

    @Override
    public String getUpdateSql() {
        String sql = "";

        if (id.equals("")) {
            sql = "insert into PersonRelationship( idPersonRelationType, idPersonA, idPersonB, dateFrom, dateTo ) values( '" + idPersonRelationType + "', '" + idPersonA + "', '" + idPersonB + "', " + validateNull(dateFrom) + ", " + validateNull(dateTo) + " )";
        } else {
            sql = "update PersonRelationship set idPersonRelationType = '" + idPersonRelationType + "', idPersonA= '" + idPersonA + "', idPersonB= '" + idPersonB + "', dateFrom= " + validateNull(dateFrom) + ", dateTo= " + validateNull(dateTo) + " where id = '" + id + "'";
        }

        return sql;
    }

    public String getIdentifier() {
        return "idPersonRelationship=" + id;
    }
}
